package com.test.nnooolwdeaer.util_wooden;

import android.content.Context;
import android.text.TextUtils;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.address.AddressUtil_Niri_wooden;
import com.test.nnooolwdeaer.util_wooden.address.AddressUtil_Viet_wooden;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StringUtil_wooden {

    public static String getSafeString_wooden(String str){
        if (TextUtils.isEmpty(str)) return "";
        return str.trim();
    }

    //获取Url后缀
    public static String getUrlSuffix_wooden(String url){
        if (TextUtils.isEmpty(url)) return "";
        String[] strs = url.split("/");
        if (strs.length<5) return "/"+strs[strs.length-1];
        StringBuilder result = new StringBuilder();
        for (int i = 4;i<strs.length;i++){
            result.append("/").append(strs[i]);
        }
        return result.toString();
    }

    //金额显示格式化
    public static String amountOfMoneyFormat_wooden(String numStr){
        StringBuilder result=new StringBuilder();
        if (numStr.indexOf('.')!=-1){
            String[] strings = numStr.split("\\.");
            result.append(".").append(strings[1]);
            numStr = strings[0];
        }
        for (int i=numStr.length()-1,count=0;i>=0;i--){
            char c = numStr.charAt(i);
            if (c>='0'&&c<='9'){
                result.insert(0,c);
                count++;
            }
            if (count%3==0&&i>0){
                result.insert(0,',');
            }
        }
        return result.toString();
    }

    //银行账户显示后四位
    public static String bankAccountHideFront_wooden(String bankAccount){
        if (TextUtils.isEmpty(bankAccount)) return "";
        StringBuilder result = new StringBuilder();
        int length = bankAccount.length();
        if (length<=4) return bankAccount;
        for (int i = 1;i<=4;i++){
            result.insert(0,bankAccount.charAt(length-i));
        }
        result.insert(0,"*****");
        return result.toString();
    }

    //银行账户隐藏中间部分
    public static String bankAccountHideMiddle_wooden(String bankAccount){
        if (TextUtils.isEmpty(bankAccount)) return "";
        StringBuilder result = new StringBuilder();
        int length = bankAccount.length();
        for (int i = 1;i<=length;i++){
            if (i<=4||length-i<3) result.insert(0,bankAccount.charAt(length-i));
            else result.insert(0,"*");
        }
        return result.toString();
    }

    public static int safeParseInt_wooden(String str){
        str = str.trim();
        if (TextUtils.isEmpty(str)) return 0;
        StringBuilder safeStr = new StringBuilder();
        for (int i = 0;i<str.length();i++){
            char c = str.charAt(i);
            if (c>='0'&&c<='9') safeStr.append(c);
            else if (c == '.') return Integer.parseInt(safeStr.toString());
        }
        return Integer.parseInt(safeStr.toString());
    }

    public static double safeParseDouble_wooden(String str){
        try {
            str = str.trim();
            if (TextUtils.isEmpty(str)) return 0;
            return Double.parseDouble(str);
        }catch (NumberFormatException e){
            if (Constant_wooden.Debug) e.printStackTrace();
        }
        return 0;
    }

    public static String parseLimit_wooden(String limit){
        String[] strings = limit.split("-");
        return strings[strings.length-1];
    }

    //解析地址
    public static void parseAddress_wooden(Context context){
        ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open("address_list_niri.json")));
                String line;
                StringBuilder stringBuilder = new StringBuilder();
                while ((line = bufferedReader.readLine())!=null){
                    stringBuilder.append(line);
                }
                bufferedReader.close();
                JSONArray jsonArray = new JSONArray(stringBuilder.toString());
                AddressUtil_Niri_wooden.getInstance_wooden().parse_wooden(jsonArray);
            }catch (Exception e){
                if (Constant_wooden.Debug) e.printStackTrace();
            }
        });

    }


    public static String clearFrontZero_wooden(String str){
        if (TextUtils.isEmpty(str)) return "0";
        else if (!str.startsWith("0")) return str;
        int startIndex = 0;
        int endIndex = str.length();
        while (startIndex<endIndex&&str.charAt(startIndex)=='0') startIndex++;
        return str.substring(startIndex,endIndex);
    }

    //A.如果有加号开头，
    //去掉所有空格与-后，如果是以+255开头：去掉+255，其它：去掉+
    //
    //B.如果非加号开头，
    //去掉所有空格与-
    public static String mobileFormat_wooden(String mobileStr){
        mobileStr = mobileStr.replaceAll(" ","");
        mobileStr = mobileStr.replaceAll("-","");
        if (mobileStr.startsWith("+225")) mobileStr=mobileStr.replaceAll("\\+225","");
        else if (mobileStr.startsWith("+")) mobileStr=mobileStr.replaceAll("\\+","");
        return mobileStr;
    }

    public static String processTitleFormat_wooden(String processName){
        if (TextUtils.isEmpty(processName)) return "";

        processName = processName.replaceAll("_"," ");
        processName = processName.substring(0,1).toUpperCase() + processName.substring(1);

        return processName;
    }

}
